package cn.tedu.onlinestore.address.service.impl;

import cn.tedu.onlinestore.address.mapper.AddressMapper;
import cn.tedu.onlinestore.address.pojo.dto.AddressParam;
import cn.tedu.onlinestore.address.pojo.entity.Address;
import cn.tedu.onlinestore.address.pojo.vo.AddressVO;
import cn.tedu.onlinestore.address.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;


    @Override
    public List<AddressVO> address(Integer id) {
        log.debug("查询收获地址业务层：{}" ,id);
        List<AddressVO> addressVOS= addressMapper.addressByUserId(id);
        return addressVOS;
    }

    @Override
    public void saveAddress(AddressParam addressParam) {
        log.debug("新增收获地址业务层：{}", addressParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressParam,address);
        if(address.getUserId() != null && address.getId() !=null){
            log.debug("修改收获地址业务层：{}", addressParam);
            address.setAddress(addressParam.getAddress());
            address.setUserId(addressParam.getUserId());
            addressMapper.updateAddress(address);
        }else{
            address.setUserId(addressParam.getUserId());
            address.setAddress(addressParam.getAddress());
            addressMapper.saveAddress(address);
        }


    }

    @Override
    public AddressVO addressById(Long id) {
        log.debug("回显收获地址业务层：{}", id);
         AddressVO addressVOS = addressMapper.addressById(id);
         return addressVOS;
    }


    @Override
    public void deleteAddress(Long addressId) {
        log.debug("删除收获地址：{}", addressId);
        addressMapper.deleteAddress(addressId);
    }
}
